package com.bobi.tfca.ui.member

import android.os.Bundle
import android.view.View
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentMemberListBinding
import com.bobi.tfca.ui.adapter.MemberListAdapter

class MemberListFragment : BaseFragment<FragmentMemberListBinding, MemberListFragmentVM>(
    FragmentMemberListBinding::inflate,
    MemberListFragmentVM::class.java,
    true
) {
    // 会员RV
    private lateinit var memberRV: RecyclerView

    override fun initFragment(
        binding: FragmentMemberListBinding,
        viewModel: MemberListFragmentVM?,
        savedInstanceState: Bundle?
    ) {
        // 初始化标题
        val groupId = publicViewModel!!.chatRoom.value!!.groupId
        val groupName = publicViewModel!!.chatRoom.value!!.groupName

        // 初始化topPanel
        setTopPanelStyle(binding, groupId, groupName)

        // 初始化RV
        initRecyclerView(binding)

        // 设置viewmodel监听
        viewModel!!.apply {
            members.observe(viewLifecycleOwner) {
                val adapter = memberRV.adapter as MemberListAdapter
                adapter.memberResponseList = it
                adapter.notifyDataSetChanged()
            }
        }
    }


    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(
        binding: FragmentMemberListBinding,
        groupId: Int,
        groupName: String?
    ) {
        val topPanel = binding.topPanel
        val leftLayout = topPanel.leftLayout
        leftLayout.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }
        val rightLayout = topPanel.rightLayout
        topPanel.rightButton.visibility = View.GONE
        rightLayout.setOnClickListener {
            // 根据群聊id跳转群聊详情
            val bundle: Bundle = Bundle()
            bundle.putInt("groupId", groupId)
            requireActivity().findNavController(R.id.app_nav)
                .navigate(R.id.action_groupchatRoomFragment_to_groupDetailFragment, bundle)
        }
        val title = topPanel.titleText
        // 群聊名称
        title.text = groupName
    }

    /**
     * 初始化RV
     */
    private fun initRecyclerView(binding: FragmentMemberListBinding) {
        // 初始化RV
        memberRV = binding.memberRV
        // 设置布局排列方式，默认垂直排列
        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        memberRV.layoutManager = linearLayoutManager
        // 添加分隔线
        memberRV.addItemDecoration(
            DividerItemDecoration(
                binding.root.context,
                LinearLayoutManager.VERTICAL
            )
        )
        // 绑定adapter
        val memberAdapter = MemberListAdapter(this, arrayListOf())
        memberRV.adapter = memberAdapter
    }

    override fun resumeFragment(viewModel: MemberListFragmentVM?) {
        super.resumeFragment(viewModel)
        val groupId = publicViewModel!!.chatRoom.value!!.groupId
        viewModel!!.apply {
            findAllGroupMembers(publicViewModel!!, groupId, this@MemberListFragment)
        }

    }
}